package 贪心算法;

import java.util.HashMap;

public class 连续数组 {
    public static int findMaxLength(int[] nums) {
        int n = nums.length;
        int[] sum = new int[n];
        for (int i = 0; i < n; i++) {
            sum[i] = (i==0? 0: sum[i-1]) + (nums[i]==0? -1: 1);
        }
        HashMap<Integer,Integer> map = new HashMap();
        map.put(0,-1);
        int length = 1;
        for (int i = 0; i < n; i++) {
            if (map.containsKey(sum[i])){
                length = Math.max((i - map.get(sum[i])),length);
            }else {
                map.put(sum[i],i);
            }
        }
        return length;
    }

    public static void main(String[] args) {
        int[] nums = {0,1,0,1,1,0};
        System.out.println(findMaxLength(nums));
    }
}
